package com.example.vote.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.vote.controller.UserController;
import com.example.vote.entity.Items;
import com.example.vote.entity.Record;
import com.example.vote.entity.Vote;
import com.example.vote.mapper.ItemsMapper;
import com.example.vote.mapper.RecordMapper;
import com.example.vote.mapper.UserMapper;
import com.example.vote.mapper.VoteMapper;
import com.example.vote.service.BizException;
import com.example.vote.service.IVoteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * <p>
 * 投票系统-题目表-2 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2024-04-14
 */
@Service
public class VoteServiceImpl extends ServiceImpl<VoteMapper, Vote> implements IVoteService {


    @Resource
    ItemsMapper itemsMapper;
    @Transactional //开启事务  2 张表操作

    public void create(Vote entity) throws BizException {
        //字段验证  拓展  实现spring 验证框架
        if(entity.getVname() == null || entity.getVname().trim().isEmpty()){
            throw new BizException("请输入标题！");
        }
        if(entity.getVtype() == null || entity.getVtype().trim().isEmpty()){
            throw new BizException("请选择类型！");
        }
        if(entity.getUsid() == null ){
            throw new BizException("请登录系统！");
        }
        if(entity.getItems() == null || entity.getItems().size() <2){
            throw new BizException("至少选择两个选项！");
        }
        //同名验证 MyBatisPlus 条件查询
        LambdaQueryWrapper<Vote> lqw = new LambdaQueryWrapper<>();
        //这个条件表示查询 usid 属性等于 entity 对象的 usid 属性值的记录。
        lqw.eq(Vote::getUsid,entity.getUsid());
        lqw.eq(Vote::getVname,entity.getVname());
        long count = count(lqw);
        if(count > 0){
            throw new BizException("您已发起过相同标题的投票，请勿重复投票！");
        }

        //保存vote表   save 完成后  Mybatis会返回  记录的主键
        if (save(entity)) {
            //保存items表
            for (Items item : entity.getItems()) {
                item.setVid(entity.getId()); //设置关联外键
                itemsMapper.insert(item);
            }
            return;
        }
        throw new BizException("新增投票失败！");


    }

    @Resource
    RecordMapper recordMapper;

    @Resource
    VoteMapper voteMapper;
    @Resource
    UserMapper userMapper;
    public void delById(int id,String name) throws BizException {
        //判断是否已投票
        LambdaQueryWrapper<Record> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Record::getVid,id);
        Long count = recordMapper.selectCount(lqw);
        int usid = userMapper.selectByid(name);
        Vote vote1 = voteMapper.selectById(id);
        System.out.println(usid);

        //该投票发起者才能删除
        if(usid == vote1.getUsid()){
            if(count > 0){
                //已投票  逻辑删除
                Vote vote = new Vote();
                vote.setId(id);
                vote.setStatus(0);
                this.updateById(vote);
            }
            else{
                //未投票  物理删除
                //先删除items表记录
                LambdaQueryWrapper<Items> lqw2 = new LambdaQueryWrapper<>();
                lqw2.eq(Items::getVid,id);
                itemsMapper.delete(lqw2);
                //再删除vote表记录
                this.removeById(id);
            }
        }
       else{
           throw new BizException("您无权删除该投票！");
      }



    }
}
